//
//  UIViewController+Base.m
//  PersonalTools
//
//  Created by Samuel on 2/9/15.
//  Copyright (c) 2015 Samuel. All rights reserved.
//

#import "UIViewController+Base.h"

@implementation UIViewController (Base)
- (UIButton*)setDefaultLeftButton
{
    UIButton *leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    [leftButton setBackgroundColor:[UIColor greenColor]];
    return leftButton;
}
- (UIButton*)setDefaultRightButton
{
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44, 44)];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [rightButton setBackgroundColor:[UIColor greenColor]];
    return rightButton;
}


- (UIButton*)setLeftButtonWithBack;
{
    UIButton *button = [self setDefaultLeftButton];
    [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchDown];
    return button;
}

- (void)back:(UIButton*)sender
{
//    if (![self isMemberOfClass:[self class]]) {
        if (self.parentViewController.childViewControllers.count>1) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else{
            if (self.presentingViewController) {
                [self dismissViewControllerAnimated:YES completion:nil];
                
            }
        }
//    }
}
@end
